using UnityEngine;

namespace Gj
{
    public class RandomRotateHelper : NodeHelper
    {
        public float waitTime = 3;
        public float speed = 100;
        private readonly float pre = 10;
        private Vector2 random = Vector2.zero;
        private bool randoming;
        private float randomTime;

        protected override BindDataType bindDataType => BindDataType.Int;

        // Update is called once per frame
        private void Update()
        {
            if (Time.time < randomTime + waitTime)
            {
                random = new Vector2(random.x - random.x / pre / waitTime, random.y - random.y / pre / waitTime);
                transform.Rotate(random.y, random.x, 0, Space.World);
            }
            else if (randoming)
            {
                randoming = false;
                Emit(UIEvent.End);
            }
        }

        protected override void BindData(int i)
        {
            if (i != randomTime && i > randomTime)
            {
                randomTime = i;
                random = new Vector2(Random.value * speed * waitTime, Random.value * speed * waitTime);
                randoming = true;
                Emit(UIEvent.Start);
            }
        }
    }
}